/** * @Auther lyh Fun 注册 * @data 2020/07/07 */
<template>
  <div class="Register">
    <header>
      <img
        src="../../../assets/homePage/scrmlogo.png"
        @click="homePage"
        alt=""
      />
    </header>
    <div class="Register_card flex">
      <!-- <div class="left_img" @click="homePage"></div> -->
      <div class="Register-con">
        <div icon="Register-in" :bordered="false">
          <div class="form-con">
            <div class="BoxTop">企业注册</div>
            <!-- <div class="activr">
              <span :class="Show == true ? 'back' : ''"></span>
              <span :class="Show == false ? 'back' : ''"></span>
            </div> -->
            <Form
              :model="formInline"
              v-show="Show"
              :rules="ruleCustom"
              ref="formCustom"
            >
              <FormItem prop="orgFullName">
                <Input
                  type="text"
                  clearable
                  v-model="formInline.orgFullName"
                  placeholder="请输入公司全称"
                />
              </FormItem>
              <FormItem prop="orgContactName">
                <Input
                  type="text"
                  clearable
                  v-model="formInline.orgContactName"
                  placeholder="请输入您的姓名"
                />
              </FormItem>
              <FormItem prop="orgContactPhone">
                <Input
                  style="width:70%"
                  autocomplete="off"
                  type="text"
                  clearable
                  v-model="formInline.orgContactPhone"
                  placeholder="请输入您的手机号"
                  @on-change="onchange('formCustom')"
                />
                <Button
                  class="buttoncode"
                  :size="buttonSize"
                  v-show="showtime"
                  style="width:30%"
                  @click="sendCode"
                  >获取验证码</Button
                >
                <Button
                  class="buttoncode"
                  :size="buttonSize"
                  style="width:30%"
                  disabled
                  v-show="!showtime"
                  type="default"
                  >{{ count }}s后重新获取</Button
                >
                <span v-show="codeShow"
                  ><Icon
                    type="ios-alert-outline"
                  />验证码已发送，十分钟内有效</span
                >
              </FormItem>
              <FormItem prop="smsCode">
                <Input
                  type="text"
                  v-model="formInline.smsCode"
                  placeholder="验证码"
                />
              </FormItem>
              <FormItem>
                <!-- <Silder
                  :successFun="successFun"
                  :errorFun="errorFun"
                  :controls="control"
                  :userPhone="formInline.orgContactPhone"
                  @smsRecordId="smsRecordIdFn"
                ></Silder> -->
              </FormItem>
              <FormItem id="fromDiv1" prop="passWord">
                <Input
                  type="password"
                  autocomplete="new-password"
                  v-model="formInline.passWord"
                  placeholder="密码（6-15位字母或数字，区分大小写）"
                  password
                />
              </FormItem>
              <div id="fromDiv" class="tomgyidl mt21">
                <span class="tongyi">
                  <Checkbox v-model="single">
                    我同意并遵守
                    <router-link :to="{ name: 'findpassword' }"
                      >《云之学服务协议》</router-link
                    >
                  </Checkbox>
                </span>
                <span class="dl"
                  ><router-link :to="{ name: 'login' }">
                    去登录></router-link
                  ></span
                >
              </div>
              <FormItem>
                <Button
                  :type="!single ? 'dashed' : 'primary'"
                  class="btndas"
                  :disabled="!single"
                  long
                  :loading="loginLoading"
                  @click="addUser('formCustom')"
                  >确认</Button
                >
              </FormItem>
            </Form>
          </div>
        </div>
      </div>
    </div>
    <div class="bot_text">
      <div class="font14 colorFFF mb12">
        云之学 — 低成本 高质量 快速为培训机构开展新业务
      </div>
      <div class="font12 colorFFF">
        备案/许可证编号为：豫ICP备20023572号
        <span> </span> 郑州云谋网络科技有限公司 版权所有
      </div>
    </div>
  </div>
</template>
<script>
import axios from "axios";
import Silder from "../../login/components/SilderLogin";
import From from "@/libs/jss/FormValidation";
export default {
  components: {
    Silder
  },
  data() {
    const name = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("公司名称不能为空"));
      } else {
        callback();
      }
    };
    const applicant = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("姓名不能为空"));
      } else {
        callback();
      }
    };
    const smsCode = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("验证码不能为空"));
      } else {
        let codes = {
          userPhone: this.formInline.orgContactPhone,
          smsCode: value,
          id: this.codeId
        };
        // axios
        //   .post(`http://192.168.2.85:8009/sms/v1/smsVerify`, codes)
        //   .then(res => {
        //     callback();
        //   });
        this.APIS.smsSendSmsCode(codes).then(res=>{
          callback()
        }).catch(err=>{

        })
      }
    };
    const password = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("密码不能为空"));
      } else {
        var reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/;
        if (reg.test(value)) {
          callback();
        } else {
          callback(new Error("密码格式错误"));
        }
      }
    };
    return {
      loginLoading:false,
      showtime: true,
      codeShow: false,
      count: "",
      timer: null,
      buttonSize: "large",
      formInline: {
        recordId: "",
        orgFullName: "",
        // string
        // 非必须
        // 企业或机构全名
        orgContactName: "",
        // string
        // 非必须
        // 联系人姓名
        orgContactPhone: "",
        // string
        // 非必须
        // 联系人电话
        smsCode: "",
        // string
        // 非必须
        // 验证码
        passWord: "",
        // string
        // 非必须
        // 密码
        platformCode: "ym_scrm",
        // string
        // 非必须
        // 平台编码，见《备注》
        regSourceId: 1,
        // number
        // 非必须
        // 注册来源ID，见备注说明
        regSourceDesc: "医保社保缴费"
        // string
        // 非必须
        // 注册来源描述，见备注说明
      },
      model1: "",
      single: false,
      Show: true,
      control: false,
      ruleCustom: {
        orgFullName: [{ validator: name, trigger: "blur" }],
        orgContactPhone: [
          { validator: From.FormValidate.Form().phone, trigger: "change" }
        ],
        orgContactName: [{ validator: applicant, trigger: "blur" }],
        smsCode: [{ validator: smsCode, trigger: "blur" }],
        password: [{ validator: password, trigger: "blur" }]
      }
    };
  },
  methods: {
    homePage() {
      // this.$router.push('homePage')
    },
    NextStop(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          this.Show = false;
        }
      });
    },
    addUser(name) {
      this.loginLoading = true
      this.$refs[name].validate(valid => {
        if (valid) {
          let datas = {
            registerType: "2",
            ...this.formInline
          };
          datas.recordId = this.codeId;
          this.APIS.enterpriseReRgister(datas)
            .then(res => {
              if (res.code === 20000) {
                this.$router.push({ name: "login" });
                this.loginLoading = false
              } else {
                // this.$Message.error(res.message)
                this.Show = true;
                this.formInline = {}; // 清空所填信息
              }
            })
            .catch(err => {});
        } else {
          this.$Message.error("必填选项不能为空!");
        }
      });
    },
    onchange(formCustom) {
      this.$refs[formCustom].validateField("phone", valid => {
        if (!valid) {
          // 表单验证通过
          this.control = true;
        }
      });
    },
    // smsRecordIdFn (val) {
    //  this.formInline.recordId = this.codeId
    // },
    sendCode() {
      let datas = { userPhone: this.formInline.orgContactPhone };
      if (datas.userPhone) {
        var regPhone = /^1[3456789]\d{9}$/;
        if (regPhone.test(datas.userPhone)) {
        this.APIS.sendCode(datas).then(res => {
              if(res.code ===20000){
              this.codeId = res.data.smsRecordId;
              this.codeShow = true;
              const TIME_COUNT = 90;
              if (!this.timer) {
                this.count = TIME_COUNT;
                this.showtime = false;
                this.timer = setInterval(() => {
                  if (this.count > 0 && this.count <= TIME_COUNT) {
                    this.count--;
                  } else {
                    this.showtime = true;
                    clearInterval(this.timer);
                    this.timer = null;
                  }
                }, 1000);
              }
              }else{
              }
            })
            .catch(err => {});
        } else {
          this.$Message.warning("手机号格式不正确");
        }
      } else {
        this.$Message.warning("输入手机号");
      }
 // axios.post(`http://192.168.2.85:8009/sms/v1/sendSmsCode`, datas)
      // axios({
      //    method:'post',
      //    url:`http://192.168.2.85:8009/sms/v1/sendSmsCode`,
      //    headers: {
      //      'Content-Type': 'application/json',//设置请求头请求格式为JSON
      //     //  'access_token': this.token //设置token 其中K名要和后端协调好
      //    },
      //    params:datas
      // }).then((response)=>{})

      // this.APIS.sendCode(datas).then(res => {
      //   if(res.code === 200000){
      //   this.$Message.success('验证码发送成功!')
      //   }else{
      //     this.$Message.success(res.err)
      //   }
      //   })
    },
    successFun() {},
    errorFun() {}
  }
};
</script>
<style lang="less" scoped>
@import "../style.less"; /*引入公共样式*/
.buttoncode {
  height: 40px;
  // border-left:0 solid rgba(200,200,200,0.25)
}
</style>
